package com.zy.qudadid.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.rey.material.widget.Button;
import com.yancy.imageselector.ImageConfig;
import com.yancy.imageselector.ImageSelector;
import com.yancy.imageselector.ImageSelectorActivity;
import com.zy.lcpassenger.R;
import com.zy.qudadid.presenter.RegisterCInfoPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.view.RegisterCInfoView;
import com.zy.qudadid.ui.widget.RoundCornerImageView;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.ActivityUtil;
import com.zy.qudadid.utils.Bun;
import com.zy.qudadid.utils.GlideLoader;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ninos on 2016/7/30.
 */
public class RegisterCInfoActivity extends ToolBarActivity<RegisterCInfoPresenter> implements RegisterCInfoView {

    private String phoneNumber = "";
    private String pwd = "";
    private String path = "";
    private String name = "";
    private String zhunjia = "";
    private String carnumber = "";
    private String lng = "";
    private String lat = "";
    private String type = "1";
    @Bind(R.id.rci_chuzuche)
    Button rci_chuzuche;
    @Bind(R.id.rci_wangyueche)
    Button rci_wangyueche;
    @Bind(R.id.rci_name)
    EditText rci_name;
    @Bind(R.id.rci_carnumber)
    EditText rci_carnumber;
    @Bind(R.id.rci_zhunjia)
    EditText rci_zhunjia;
    @Bind(R.id.rci_company)
    EditText rci_company;
    @Bind(R.id.rci_cartype)
    EditText rci_cartype;
    @Bind(R.id.rci_image)
    RoundCornerImageView rci_image;
    @Bind(R.id.ctnumber)
    RelativeLayout ctnumber;
    @Bind(R.id.ctinfo)
    LinearLayout ctinfo;

    @OnClick(R.id.rci_image)
    public void rci_image() {
        ImageConfig imageConfigimg
                = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
                .titleBgColor(getResources().getColor(R.color.colorPrimary))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                // (截图默认配置：关闭    比例 1：1    输出分辨率  500*500)
                .crop(1, 1, 500, 500)
                // 开启单选   （默认为多选）
                .singleSelect()
                // 开启拍照功能 （默认关闭）
                .showCamera()
                // 拍照后存放的图片路径（默认 /temp/picture） （会自动创建）
                .filePath("/ImageSelector/Pictures")
                .build();


        ImageSelector.open(this, imageConfigimg);   // 开启图片选择器
    }

    @OnClick(R.id.rci_chuzuche)
    public void rci_chuzuche() {

        startActivity(RegisterNUploadActivity.class);
        return;

//        if (rci_name.getText().toString().trim().equals("")) {
//            toast("姓名不能为空");
//            return;
//        }
//        if (rci_carnumber.getText().toString().trim().equals("")) {
//            toast("车牌号不能为空");
//            return;
//        }
//        if (rci_zhunjia.getText().toString().trim().equals("")) {
//            toast("准架证不能为空");
//            return;
//        }
//        if (path.toString().trim().equals("")) {
//            toast("用户头像不能为空");
//            return;
//        }
//        if (rci_cartype.getText().toString().trim().equals("")) {
//            toast("车型不能为空");
//            return;
//        }
//        if (rci_company.getText().toString().trim().equals("")) {
//            toast("所属公司不能为空");
//            return;
//        }
//        name = rci_name.getText().toString().trim();
//        carnumber = rci_carnumber.getText().toString().trim();
//        zhunjia = rci_zhunjia.getText().toString().trim();
//
//        Map map = new HashMap();
//        map.put("mobile", phoneNumber);
//        map.put("lng", lng);
//        map.put("lat", lat);
//        map.put("realname", name);
//        map.put("carnumber", carnumber);
//        map.put("jiashihao", zhunjia);
//        map.put("password", pwd);
//        map.put("type", type);
//        map.put("areaname", "临沂市");
//        map.put("company", rci_company.getText().toString());
//        map.put("brand", rci_cartype.getText().toString());
//        String json = StringUtil.toJson(map);
//        try {
//            String data = AESCrypt.getInstance().encrypt(json);
//            String d = StringUtil.replaceBlank(data);
//            presenter.Register(d);
//        } catch (Exception ex) {
//        }

    }

    @OnClick(R.id.rci_wangyueche)
    public void rci_wangyueche() {

//        startActivity(RegisterNUploadActivity.class);
//        return;

        if (rci_name.getText().toString().trim().equals("")) {
            toast("姓名不能为空");
            return;
        }
        if (rci_carnumber.getText().toString().trim().equals("")) {
            toast("车牌号不能为空");
            return;
        }
        if (rci_zhunjia.getText().toString().trim().equals("")) {
            toast("准架证不能为空");
            return;
        }
        if (path.toString().trim().equals("")) {
            toast("用户头像不能为空");
            return;
        }
        if (rci_cartype.getText().toString().trim().equals("")) {
            toast("车型不能为空");
            return;
        }
        name = rci_name.getText().toString().trim();
        carnumber = rci_carnumber.getText().toString().trim();
        zhunjia = rci_zhunjia.getText().toString().trim();

        Map map = new HashMap();
        map.put("mobile", phoneNumber);
        map.put("lng", lng);
        map.put("lat", lat);
        map.put("realname", name);
        map.put("carnumber", carnumber);
        map.put("jiashihao", zhunjia);
        map.put("password", pwd);
        map.put("type", 2);
        map.put("areaname", "所属地区");
        map.put("company", "");
        map.put("brand", rci_cartype.getText().toString());

//        map.put("mobile", phoneNumber);
//        map.put("lng", lng);
//        map.put("lat", lat);
//        map.put("realname", name);
//        map.put("carnumber", carnumber);
//        map.put("jiashihao", zhunjia);
//        map.put("brand", rci_cartype.getText().toString());
//        map.put("password", pwd);
        String json = StringUtil.toJson(map);
        try {
            String data = AESCrypt.getInstance().encrypt(json);
            String d = StringUtil.replaceBlank(data);
            presenter.RegisterDriver(d);
        } catch (Exception ex) {
        }

    }

    @Override
    protected CharSequence provideTitle() {
        return "基本信息";
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {

            // Get Image Path List
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            Bitmap bitmap = BitmapFactory.decodeFile(pathList.get(0));
            path = pathList.get(0);
            Glide.with(this)
                    .load(path)
                    .centerCrop()
                    .crossFade()
                    .placeholder(R.drawable.userimg)
                    .into(rci_image);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register_c_info;
    }

    @Override
    public void initListeners() {

    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        try {
            if (getIntent().getBundleExtra("data").getString("type").equals("2")) {
                lat = "0.0";
                lng = "0.0";
                pwd = "修改信息";
                type = "2";
                phoneNumber = new UserUtil(getContext()).getUser().mobile;
            } else {
                phoneNumber = getIntent().getBundleExtra("data").getString("phoneNumber");
                pwd = getIntent().getBundleExtra("data").getString("pwd");
                lat = getIntent().getBundleExtra("data").getString("lat");
                lng = getIntent().getBundleExtra("data").getString("lng");
            }
        } catch (Exception ex) {
            phoneNumber = getIntent().getBundleExtra("data").getString("phoneNumber");
            pwd = getIntent().getBundleExtra("data").getString("pwd");
            lat = getIntent().getBundleExtra("data").getString("lat");
            lng = getIntent().getBundleExtra("data").getString("lng");
        }

    }

    @Override
    public RegisterCInfoPresenter createPresenter() {
        return new RegisterCInfoPresenter();
    }

    @Override
    public void success() {
        toast("信息资料提交成功，请继续完成下一步");
        ActivityUtil.addActivity(this);
        startActivity(RegisterCUploadActivity.class, new Bun().putString("phoneNumber", phoneNumber).putString("pwd", pwd).putString("path", path).putString("name", name).putString("carnumber", carnumber).putString("zhunjia", zhunjia).ok());
    }

    @Override
    public void error(String msg) {
        toast(msg);
    }

    @Override
    public void exception() {

    }
}
